#include <iostream>
#include <vector>
using namespace std;
class Solution {
    int COLOR, M, N, OLD;

public:
    void dfs(int row, int col, vector<vector<int>>& grid, vector<vector<int>>& vis)
    {
        if (vis[row][col] != 0)
            return;
        else
        {
            if (row == 0 || row == M - 1 || col == 0 || col == N - 1)
            {
                if (grid[row][col] == OLD)
                {
                    vis[row][col] = 2;
                    grid[row][col] = COLOR;
                }
            }
            else if (grid[row - 1][col] != OLD && vis[row - 1][col] != 2)
            {
                vis[row][col] = 2;
                grid[row][col] = COLOR;
            }
            else if (grid[row + 1][col] != OLD && vis[row + 1][col] != 2)
            {
                vis[row][col] = 2;
                grid[row][col] = COLOR;
            }
            else if (grid[row][col - 1] != OLD && vis[row][col - 1] != 2)
            {
                vis[row][col] = 2;
                grid[row][col] = COLOR;
            }
            else if (grid[row][col + 1] != OLD && vis[row][col + 1] != 2)
            {
                vis[row][col] = 2;
                grid[row][col] = COLOR;
            }
            else
            {
                vis[row][col] = 1;
            }
            if (row > 0 && grid[row - 1][col] == OLD && vis[row - 1][col] == 0)
            {
                dfs(row - 1, col, grid, vis);
            }
            if (row < M - 1 && grid[row + 1][col] == OLD && vis[row + 1][col] == 0)
            {
                dfs(row + 1, col, grid, vis);
            }
            if (col > 0 && grid[row][col - 1] == OLD && vis[row][col - 1] == 0)
            {
                dfs(row, col - 1, grid, vis);
            }
            if (col < N - 1 && grid[row][col + 1] == OLD && vis[row][col + 1] == 0)
            {
                dfs(row, col + 1, grid, vis);
            }
        }
    }

    vector<vector<int>> colorBorder(vector<vector<int>>& grid, int row, int col, int color) {
        M = grid.size(), N = grid[0].size(), COLOR = color, OLD = grid[row][col];
        vector<vector<int>> vis(M, vector<int>(N, 0));
        dfs(row, col, grid, vis);
        return grid;
    }
};
int main()
{
	return 0;
}